package com.chen.sa.auth.config;

import cn.dev33.satoken.sso.template.SaSsoServerTemplate;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.root.entity.SysComConfig;
import com.chen.sa.auth.mapper.SysComConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 重写 SaSsoServerTemplate 部分方法，增强功能 
 */
@Slf4j
@Component
public class CustomSaSsoServerTemplate extends SaSsoServerTemplate {

    @Resource
    private SysComConfigMapper sysComConfigMapper;

    /** 重写 [获取授权回调地址] 方法，改为从数据库中读取 */
    @Override
    public String getAllowUrl() {
        List<SysComConfig> list = sysComConfigMapper.selectList(Wrappers.lambdaQuery(SysComConfig.class)
                .select(SysComConfig::getValue)
                .eq(SysComConfig::getGroupName, "allow_url_group")
        );
        String allowUrlStr = list.stream().map(SysComConfig::getValue).collect(Collectors.joining(","));
        log.info("授权回调地址: {}", allowUrlStr);
        return allowUrlStr;
    }

}
